﻿#region Librerias usadas
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.IO;
using System.Web;
using System.Windows.Forms;
#endregion


namespace Librerias
{
     /// <summary>
     /// Clase que nos proveerá de la cadena de conexión
     /// </summary>
     public class CADConexion
     {
         /// <summary>
         /// Variable que contiene la cadena de conexión hacia nuestro servidor
         /// </summary>
        //public static String connString = "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=\"..\\..\\eCampusWeb\\App_Data\\bdEcampus.mdf\";User Instance=true"; //@"Data Source=grupo12.no-ip.org\SQLEXPRESS;User ID=ecampusUser2012;Password=@tarantula2012";
         public static String connString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"|DataDirectory|\\bdEcampus.mdf\";Integrated Security=True;User Instance=True";
                                         
         #region Interfaz Pública
         /// <summary>
         /// Nos permite realizar una consulta a nuestra BD
         /// </summary>
         /// <param name="sql">Comando sql</param>
         /// <param name="databaseConnection">Conexión SQL</param>
         /// <param name="resultDataSetUsu">DATASET que nos pasan y devolvemos relleno</param>
         /// <param name="tabla">Tabla a la que ataca la consulta</param>
         /// <returns></returns>
         public int Consultar(string sql, SqlConnection databaseConnection, ref DataSet resultDataSetUsu, string tabla)
         {
            string strSQLSelect = sql;
            int filas = 0;
            SqlCommand selectCommand = new SqlCommand(strSQLSelect, databaseConnection);
            SqlDataAdapter dsCmd = new SqlDataAdapter();
            SqlCommandBuilder custCB = new SqlCommandBuilder(dsCmd);

            selectCommand.CommandType = CommandType.Text;

            try
            {
                dsCmd.SelectCommand = selectCommand;
                filas = dsCmd.Fill(resultDataSetUsu, tabla);
            }
            catch (Exception ex)
            {
                filas = -1;
                throw new System.Exception("ERROR 002: Imposible realizar la consulta\n" + ex.Message.ToString());
            }
            return filas;
        }  
        #endregion 
     }
}
